package com.woniuxy.cloud.carl.store.app.apiImpl;


import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.store.app.utils.JwtUtil;
import com.woniuxy.cloud.carl.store.client.StoreClient;
import com.woniuxy.cloud.carl.store.client.param.*;
import com.woniuxy.cloud.carl.store.client.result.StoreDto;
import com.woniuxy.cloud.carl.store.common.StoreScoreRedisModel;
import com.woniuxy.cloud.carl.store.infra.model.Store;
import com.woniuxy.cloud.carl.store.service.StoreService;
import com.woniuxy.result.Result;
import io.seata.spring.annotation.GlobalTransactional;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@RestController
public class StoreAPIImpl implements StoreClient {
    @Autowired
    StoreService storeService;
    @Autowired
    RedissonClient redissonClient;
//    @Autowired
//    JwtUtil jwtUtil;
    @Override
    @Log("添加门店信息")
    public Result addStore(AddStoreParam param) {
        return storeService.addStore(param);
    }

    @Override
    @Log("修改门店信息")
    public Result modifyStore(ModifyStoreParam param) {
        return storeService.modifyStore(param);
    }

    @Override
    @Log("修改门店登录密码")
    public Result modifyStorePassword(ModifyStorePasswordParam param) {
        return storeService.modifyStorePassword(param);
    }

    @Override
    @Log("修改门店状态")
    public Result modifyStoreState(ModifyStoreStateParam param) {
        return storeService.modifyStoreState(param);
    }

    @Override
    @Log("查询门店")
    public Result<List<StoreDto>> getStore(GetStoreParam param) {
        return storeService.getStore(param);
    }

    @Override
    @Log("门店登录")
    public Result<String> storeLogin(StoreLoginParam param) {
        Result<Long> longResult = storeService.storeLogin(param);
        Long storeId = longResult.getData();
        HashMap<String, Object> map = new HashMap<>();
        map.put("sub",storeId);
        map.put("role",2l);
        Integer time=30;
        String token = JwtUtil.createToken(map, time);
        return Result.success(token);
    }

    @Override
    @Scheduled(cron = "0 0 0 1 * ?")
    public void synchronizingStoreScore() {
        RMap<Long, StoreScoreRedisModel> map = redissonClient.getMap("store:score");
        Set<Long> longs = map.keySet();
        Store store = new Store();
        for (Long aLong : longs) {
            StoreScoreRedisModel storeScoreRedisModel = map.get(aLong);
            Double score = storeScoreRedisModel.getScore();
            Integer count = storeScoreRedisModel.getCount();
            DecimalFormat decimalFormat = new DecimalFormat("#0.0");
            String format = decimalFormat.format(score / count);
            store.setStoreId(aLong);
            store.setGrade(Double.valueOf(format));
            storeService.updateById(store);
        }
        Long[] storIds ={};
        Long[] array = longs.toArray(storIds);
        map.fastRemove(array);
    }

    @Override
    @Log("修改门店服务范围")
    @GlobalTransactional
    public Result modifyServiceInStore(AddServiceInStoreParam param) {

        return storeService.modifyServiceInStore(param);
    }

    @Override
    @Log("查询门店服务范围")
    public Result<List<Long>> getServiceByStoreId(Long storeId) {
        return storeService.getServiceByStoreId(storeId);
    }


}

